// run: $exec < input
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cctype>
#include <utility>

struct data
{
	data(int first, std::string second) : first(first), second(second) {}
	data(){}
	int first; std::string second;
};

bool operator<(data const & a, data const & b)
{
	return a.first > b.first || (a.first == b.first && a.second < b.second);
}

std::string old_begin = "<oldwords>";
std::string old_end = "</oldwords>";
std::string new_begin = "<article>";
std::string new_end = "</article>";
std::string s;
std::set<std::string> readed;
std::map<std::string, int> count;

void init()
{
	readed.clear();
	count.clear();
}

void add(std::string const & s)
{
	std::string ts;
	for (int i = 0; i < (int)s.size(); i++) {
		if (!std::isalpha(s[i])) {
			if (!ts.empty()) readed.insert(ts);
			ts.clear();
		} else
			ts += char(std::tolower(s[i]));
	}
	if (!ts.empty()) readed.insert(ts);
}

void count_world(std::string const & s)
{
	std::string ts;
	for (int i = 0; i < (int)s.size(); i++) {
		if (!std::isalpha(s[i])) {
			if (!ts.empty() && readed.find(ts) == readed.end()) count[ts]++;
			ts.clear();
		} else
			ts += char(std::tolower(s[i]));
	}
	if (!ts.empty() && readed.find(ts) == readed.end()) count[ts]++;
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		init();
		std::cin >> s;
		for (; std::cin >> s && s != old_end; ) add(s);
		std::cin >> s;
		for (; std::cin >> s && s != new_end; ) count_world(s);

		std::vector<data> lis;
		for (auto i : count) lis.push_back(data(i.second, i.first));
		std::sort(lis.begin(), lis.end());
		for (int i = 0; i < std::min(10, (int)lis.size()); i++)
			std::cout << lis[i].second << '\n';
		std::cout << '\n';
	}
}

